import { atom, selector } from 'recoil'
import { useLocation } from 'react-router-dom'
import { getInfo, getSelectTree } from '@/pages/user/login/service/service'
// import { PromiseData } from '@/@types/request'
import { convertDataToCascaderFormat, isNullData } from '@/utils'

type platformDetailsInfoType = {
  platformId: number // 平台项目 ID
  announcement: string
  chineseName: string
  operationGuide: string
  overview: string
  manageScope: number
  logo: string
}
// 平台详情 --- logo 概况通知 platformId 项目 ID 等
export const platformDetailsInfo = atom({
  key: 'platformDetailsInfo',
  default: <platformDetailsInfoType>{},
})

// md 持久存储
export const platformDetailsInfoSelect = selector({
  key: 'platformDetailsInfoSelect',
  get: async (): Promise<platformDetailsInfoType> => {
    const location = useLocation()
    console.log('location🚀🚀🚀🚀')
    // const res = await getInfo({ website: window.location.origin })

    const res = await getInfo({ website: 'http://192.168.120.148:8012' })
    return res.data
  },
})

export const proviencsDataSelectorTwo = selector({
  key: 'proviencsDataSelectorTwo',
  get: async ({ get }): Promise<any> => {
    const newlist = await get(platformDetailsInfo)
    console.log('newlistnewlistnewlistnewlistnewlistnewlist🚀🚀🚀🚀', newlist)
    const res = await getSelectTree({ mangeScopend: newlist.manageScope })
    // console.log('看一下三级联动数据是什么样子', res.data)
    if (isNullData(res)) {
      return convertDataToCascaderFormat(res.data)
    }
    return []
  },
})
